package com.tanhua.server.controller;

import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.logging.Handler;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private OssTemplate ossTemplate;

    /**
     * 保存用户信息
     *  UserInfo
     *  请求头携带token
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo,
                                       @RequestHeader("Authorization") String token){
        userInfo.setId(UserHolder.getUserId());
        //3.调用service
        userInfoService.save(userInfo);
        return ResponseEntity.ok(null);
    }
    /**
     * 上传用户头像
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity head(MultipartFile headPhoto,
                               @RequestHeader("Authorization") String token) throws IOException {
        //3.调用service
        userInfoService.updateHead(headPhoto,UserHolder.getUserId());
       // String imgUrl = ossTemplate.upload(headPhoto.getOriginalFilename(), headPhoto.getInputStream());
        return ResponseEntity.ok(null);
    }

}
